// websocket.js

// export const createWebSocket = (url, onOpen, onMessage, onError, onClose) => {
//     const ws = new WebSocket(url)
//     ws.onopen = onOpen || (() => {})
//     ws.onmessage = onMessage || (() => {})
//     ws.onerror = onError || (() => {})
//     ws.onclose = onClose || (() => {})
//     return ws
// }
  


export default function userSocket(handleMessage: any) {
    const token = window.sessionStorage.getItem('token') || ''
    let t = ''
    if (token) {
        t = 'Bearer ' + token; // 每个请求携带自定义 token
    }
    const ws = new WebSocket('ws://127.0.0.1:3008/api')

    const init = () => {
        handleEvent()
    }

    const handleEvent = () => {
        ws.addEventListener('open', handleOpen, false)
        ws.addEventListener('close', handleClose, false)
        ws.addEventListener('error', handleError, false)
        ws.addEventListener('message', handleMessage, false)
    }
    const handleOpen = (e: any) => {
        console.log('open', e)
    }
    const handleClose = (e: any) => {
        console.log('close', e)
    }
    const handleError = (e: any) => {
        console.log('error', e)
    }

    init()
    return
}